<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['slip:slip:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['slip:slip:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['slip:slip:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="slipList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="工资条ID" align="center" prop="id" /> -->
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="employeeNumber" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="职位名称" align="center" prop="posName" />
      <el-table-column label="加班时长(小时)" align="center" prop="overtimeHours" />
      <el-table-column label="请假时长(小时)" align="center" prop="leaveHours" />
      <el-table-column label="基本工资" align="center" prop="basicSalary" />
      <el-table-column label="岗位工资" align="center" prop="positionSalary" />
      <el-table-column label="绩效工资" align="center" prop="performanceSalary" />
      <el-table-column label="加班补贴" align="center" prop="overtimeAllowance" />
      <el-table-column label="请假扣款" align="center" prop="leaveDeduction" />
      <el-table-column label="个人所得税" align="center" prop="incomeTax" />
      <el-table-column label="社保扣除" align="center" prop="socialInsurance" />
      <el-table-column label="公积金扣除" align="center" prop="housingFund" />
      <el-table-column label="奖金" align="center" prop="bonus" />
      <el-table-column label="实发工资" align="center" prop="totalSalary" />
      <el-table-column label="工资月份" align="center" prop="salaryMonth" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slip:slip:edit']">核算</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slip:slip:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Slip">
import { listSlip, getSlip, delSlip, addSlip, updateSlip } from "@/api/slip/slip"

const { proxy } = getCurrentInstance()

const slipList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
  }
})

const { queryParams } = toRefs(data)

/** 查询员工工资条明细列表 */
function getList() {
  loading.value = true
  listSlip(queryParams.value).then(response => {
    slipList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  const form = {
    userId: null
  }
  addSlip(form).then(response => {
    proxy.$modal.msgSuccess("新增成功")
    getList()
  })
}

/** 核算按钮操作 */
function handleUpdate(row) {
  const _ids = row ? [row.id] : ids.value
  if (!_ids || _ids.length === 0) {
    proxy.$modal.msgError("请选择需要核算的数据")
    return
  }
  
  proxy.$modal.confirm('是否确认核算员工工资条明细编号为"' + _ids.join(',') + '"的数据项？').then(function() {
    return updateSlip(_ids.join(','))
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("核算成功")
  }).catch(() => {})
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除员工工资条明细编号为"' + _ids + '"的数据项？').then(function() {
    return delSlip(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('slip/slip/export', {
    ...queryParams.value
  }, `slip_${new Date().getTime()}.xlsx`)
}

getList()
</script>
